package com.ysd.lis.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.aspect.OperationType;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.SysPoints;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.mapper.sys.SysPointsMapper;
import com.ysd.lis.request.PointsPageParam;
import com.ysd.lis.service.SysPointsService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * cold_chain.sys_points 前端控制器
 * </p>
 *
 * @author Bai
 * @since 2023-10-23
 */
@Api(tags = "功能点管理")
@RestController
public class SysPointsController extends BaseController {

    @Autowired
    SysPointsService sysPointsService;
    @Autowired
    SysPointsMapper sysPointsMapper;

    @ApiOperation("分页获取功能点信息")
    @PostMapping("/api/sys/pointsPageList")
    public Result pointsPageList(@RequestBody PointsPageParam pointsPageParam) {
        // return sysPointsService.findPointsList(pointsPageParam);
        Map returnMap = new HashMap<>();
        LambdaQueryWrapper<SysPoints> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<SysPoints> page = new Page<SysPoints>();
        page.setCurrent(pointsPageParam.getPageIndex());
        page.setSize(pointsPageParam.getPageSize());
        lambdaQueryWrapper.eq(SysPoints::getDelFlag, 0);
        SysUser sysUser = RedisUserManager.getUser();
        if (StringUtils.isNotBlank(sysUser.getOrgId())) {
            lambdaQueryWrapper.eq(SysPoints::getOrgId, sysUser.getOrgId());
        }
        if (StringUtils.isNotBlank(pointsPageParam.getModuleId())) {
            lambdaQueryWrapper.eq(SysPoints::getModuleId, pointsPageParam.getModuleId());
        }

        if (StringUtils.isNotBlank(pointsPageParam.getKeywords())) {
            lambdaQueryWrapper.like(SysPoints::getTitle, pointsPageParam.getKeywords()).or().like(SysPoints::getCode, pointsPageParam.getKeywords());
        }

        Page<SysPoints> modulePageList = sysPointsMapper.selectPage(page, lambdaQueryWrapper);
        returnMap.put("modulePageList", modulePageList);
        return Result.succ(1, "获取信息成功", returnMap);
    }



    @ApiOperation("添加功能点")
    @PostMapping("/api/sys/addPoints")
    @LogDetail(requestParam ="addPoints",operationType = OperationType.INSERT, module = "功能点管理",content = "添加功能点操作")
    public Result addPoints(@RequestBody SysPoints sysPoints) {
        Result result = new Result();
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isEmpty(sysUser)) {
                return result.fail("从Redis获取用户数据为NULL: 请检查redis相关配置");
            }
            if (ToolsUtils.isEmpty(sysUser.getOrgId())) {
                return result.fail("获取机构ID异常 ： 请给登录用户设置所属机构");
            }
            sysPoints.setOrgId(sysUser.getOrgId());
        } catch (Exception e) {
            // 处理异常情况
            result.fail("从Redis获取用户数据出现异常: 请检查redis相关配置");
        }
        return sysPointsService.addPoints(sysPoints);
    }

    @ApiOperation("修改功能点")
    @PostMapping("/api/sys/editPoints")
    @LogDetail(requestParam ="editPoints",operationType = OperationType.UPDATE, module = "功能点管理",content = "修改功能点操作")
    public Result editPoints(@RequestBody SysPoints sysPoints) {
        return sysPointsService.editPoints(sysPoints);
    }

    @ApiOperation("删除功能点")
    @DeleteMapping("/api/sys/deletePoints/{id}")
    @LogDetail(requestParam ="deletePoints",operationType = OperationType.DELETE, module = "功能点管理",content = "删除功能点操作")
    public Result deletePoints(@PathVariable("id") String id) {
        return sysPointsService.deletePoints(id);
    }


    @ApiOperation("获取模块与功能树形结构")
    @PostMapping("/api/sys/getModuleAndPointList")
    public Result getModuleAndPointList() {
        return sysPointsService.getModuleAndPointList();
    }

    @ApiOperation("获取所有功能信息（不分页)")
    @GetMapping("/api/sys/getPointList")
    public Result getPointList() {
        LambdaQueryWrapper<SysPoints> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPoints::getDelFlag,0);
        SysUser sysUser = RedisUserManager.getUser();
        if (StringUtils.isNotBlank(sysUser.getOrgId())) {
            lambdaQueryWrapper.eq(SysPoints::getOrgId, sysUser.getOrgId());
        }
        return Result.succ(sysPointsMapper.selectList(lambdaQueryWrapper));
    }





}
